package org.fjsei.yewu.job.service;

import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import lombok.extern.slf4j.Slf4j;

import org.fjsei.yewu.job.repository.SplJob;
import org.fjsei.yewu.job.repository.SplJobRepository;
import org.quartz.JobKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.function.Function;

/**
 * 【问题】 Hibernate-Search需要创建sequence hsearch_outbox_event_generator；而对于CRDB使用它过于昂贵，能否改进改用UUID替换？
* */
@Slf4j
@Service
public class SplitTask {

    @PersistenceContext
    private EntityManager entityManager;
//    @PersistenceContext
//    private EntityManager emSei;                //EntityManager相当于hibernate.Session：

    @Autowired protected SplJobRepository splJobRepository;
    static Instant  beginTime;      //任务启动时；


    public SplitTask() {

    }

    @PostConstruct
    private void init() {
    }

    @Transactional
    public List<SplJob> commitSplit(List<SplJob> batch) {
//        if(!entityManager.isJoinedToTransaction())      entityManager.joinTransaction();
//        Assert.isTrue(entityManager.isJoinedToTransaction(),"没emFjtj Transaction");
       return splJobRepository.saveAll(batch);
    }

    @Transactional
    public SplJob commitSplit(SplJob batch) {
//        if(!entityManager.isJoinedToTransaction())      entityManager.joinTransaction();
//        Assert.isTrue(entityManager.isJoinedToTransaction(),"没emFjtj Transaction");
        return splJobRepository.save(batch);
    }
    @Transactional
    public SplJob commitSplit5(JobKey jobKey) {
//        if(!entityManager.isJoinedToTransaction())      entityManager.joinTransaction();
//        Assert.isTrue(entityManager.isJoinedToTransaction(),"没emFjtj Transaction");
        SplJob splJob= splJobRepository.findByGroupNameAndJobName(jobKey.getGroup(),jobKey.getName());
        splJob.setOffs(3120298L);
        return splJobRepository.save(splJob);
    }
    @Transactional
    public SplJob commitSplit9(String group, String name) {
//        if(!entityManager.isJoinedToTransaction())      entityManager.joinTransaction();
//        Assert.isTrue(entityManager.isJoinedToTransaction(),"没emFjtj Transaction");
        SplJob splJob= splJobRepository.findByGroupNameAndJobName(group,name);
        splJob.setOffs(3120298L);
        return splJobRepository.save(splJob);
    }

    public List<SplJob> trySplit(String request) {
        List<SplJob> batch=new ArrayList<>();
        for(int i =0; i<1; i++)
        {
            SplJob ado=SplJob.builder().start(null)
                    .groupName("device").jobName("infsyn-"+22).limt(12450).offs(0L)
                    .build();
            batch.add(ado);
        }
        List<SplJob> list=commitSplit(batch);     //不管读取ID的过程，单独用小的事务提交
        log.info("trySplit: countall={}",list.size());
//        SplJob splJob= splJobRepository.findByGroupNameAndJobName("device","infsyn-22");
        return list;
    }

}
